import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight)
camera.position.set(2,2,2)
scene.add(camera)

const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

document.body.appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

const light = new THREE.AmbientLight()
scene.add(light)

const geometry = new THREE.BoxGeometry(1,1,1)
const material = new THREE.MeshBasicMaterial({ color: 'orange' })

const mesh = new THREE.InstancedMesh(geometry, material, 100)

for(let i =0; i < 100; i++) {
  mesh.setColorAt(i, new THREE.Color().setHex(Math.random() * 0xffffff))
  const matrix = new THREE.Matrix4()
  matrix.compose(
    new THREE.Vector3((Math.random() - 0.5) * 4, (Math.random() - 0.5) * 4, (Math.random() - 0.5) * 4),
    new THREE.Quaternion(),
    new THREE.Vector3(0.3, 0.3, 0.3)
  )
  mesh.setMatrixAt(i, matrix)
}

scene.add(mesh)


const tick = () => {
  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(tick)
}

tick()
